Bug 7132889

Bug Description:

This is a race bug. Expected exception is : java.lang.RuntimeException: Key is valid
More details about this bug are at JDK-7132889 JIRA page.

Interleaving Description:

java.nio.channels.spi.AbstractInterruptibleChannel:
t1  public final void close() throws IOException {
        synchronized (closeLock) {
1           if (!open)
                return;
3           open = false;
4           implCloseChannel();
        }
    }
    
java.nio.channels.spi.AbstractSelectableChannel:
t2  public final SelectionKey register(Selector sel, int ops,
                       Object att)
    throws ClosedChannelException
    {
2       if (!isOpen())
            throw new ClosedChannelException();
        if ((ops & ~validOps()) != 0)
            throw new IllegalArgumentException();
        synchronized (regLock) {
            if (blocking)
                throw new IllegalBlockingModeException();
            SelectionKey k = findKey(sel);
            if (k != null) {
                k.interestOps(ops);
                k.attach(att);
            }
            if (k == null) {
                // New registration
                k = ((AbstractSelector)sel).register(this, ops, att);
5               addKey(k);
            }
6           return k;
        }
    }

a) thread 1 checks channel is open at 1.
b) Before thread 1 set open to false at 3, context switched, thread 2 passes the check of open at 2.
c) context switched, thread 1 set open to false at 3 and close the channel at 4.
d) context switched, thread 2 successfully registered the key through 5 and 6.
Although channel has been closed, key is still registered successfully.

How To Reproduce:

This bug is reproduced under JDK 1.6.0.
It started at JDK 1.6.0, and has been fixed since JDK 1.7.0_41.
Execute the following scripts to run the test to reproduce the bug (assume the location of the jdk6 test project is jdk_test_home).

Linux:
${jdk_test_home}/scripts/7132889.sh [--javaloc path]
Windows:
%jdk_test_home%\scripts\7132889.bat [--javaloc path]

Option Function
--javaloc The location of JDK that is eligible to reproduce the bug, if your java environment is not eligible.
In this case, it's JDK 1.6.0. It should be the absolute path to the JDK's java starter and ended with "/".
For example: ~/jdk/home/jdk1.6.0/bin/